﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;

namespace AutoCs.Keyboard
{
    public class Input
    {
        public static void KeyDownOnce(IntPtr intPtr, KeyCode keyCode) {
            CsSharpApi.PostMessage(intPtr, CsSharpApi.WM_KEYDOWN, (int)keyCode, 0);
            CsSharpApi.PostMessage(intPtr, CsSharpApi.WM_KEYUP, (int)keyCode, 0);
        }
        public static void KeyDownOnce(KeyCode keyCode)
        {
            CsSharpApi.keybd_event((byte)keyCode, 0, 0, 0);
        }
        public static void KeyGroupDown(KeyCode[] keyCodes) {
            CsSharpApi.keybd_event((byte)keyCodes[0], 0, 0, 0);
            CsSharpApi.keybd_event((byte)keyCodes[1], 0, 0, 0);
            CsSharpApi.keybd_event((byte)keyCodes[1], 0, CsSharpApi.KEYEVENTF_KEYUP, 0);
            CsSharpApi.keybd_event((byte)keyCodes[0], 0, CsSharpApi.KEYEVENTF_KEYUP, 0);

        }
        public static void KeyGroupDown(IntPtr intPtr, KeyCode[] keyCodes)
        {
            if (keyCodes.Length < 2)
            {
                Console.WriteLine("无效的组合按键");
                return;
            }
            IntPtr target = CsSharpApi.GetWindowThreadProcessId(intPtr,IntPtr.Zero);
            IntPtr mythread = CsSharpApi.GetCurrentThreadId();
            bool Iret = CsSharpApi.AttachThreadInput(mythread, target,true);
            if (!Iret)
            {
                Console.WriteLine("尝试更正");
                CsSharpApi.BringWindowToTop(intPtr);
                CsSharpApi.SetForegroundWindow(intPtr);
                Iret = CsSharpApi.AttachThreadInput(mythread, target, true);
                Console.WriteLine(Iret);
            }
            IntPtr intPtr1 = CsSharpApi.SetFocus(intPtr);
            if (intPtr1 != IntPtr.Zero) { Console.WriteLine("截取键盘焦点成功"); }
            else Console.WriteLine("截取键盘焦点失败");
            KeyGroupDown(keyCodes);
        }

        public static int GetKeyIParam(byte scanCode, byte keyUpOrDown, byte isRelease, byte fAltDown = 0, byte downTimes = 0)
        {
            byte LeftOrRight = fAltDown;
            return (isRelease << 31) | (keyUpOrDown << 30) | (fAltDown << 29) | (LeftOrRight << 24) | (scanCode << 16) | downTimes;
        }

        public static void MouseClick(IntPtr intPtr,int x,int y) {
            int param = x + (y << 16);
            CsSharpApi.SendMessage(intPtr, CsSharpApi.WM_MOUSEACTIVATE, 0x230958, 0x2010002);
            CsSharpApi.SendMessage(intPtr, CsSharpApi.WM_LBUTTONDOWN, 1, 0xee0363);
            Thread.Sleep(50);
            CsSharpApi.SendMessage(intPtr, CsSharpApi.WM_LBUTTONUP, 1, 0xee0363);
            Thread.Sleep(50);
            CsSharpApi.SendMessage(intPtr, CsSharpApi.WM_MOUSEMOVE, 0, 0xee0363);
        }
        /// <summary>
        /// 前台模拟鼠标左键点击
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static void MouseClick(int x, int y)
        {
            CsSharpApi.mouse_event(CsSharpApi.MOUSEEVENTF_LEFTDOWN | CsSharpApi.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }
        public static void MouseMoveTo(int x, int y,bool IsAbsolute) {

            if(IsAbsolute)CsSharpApi.mouse_event(CsSharpApi.MOUSEEVENTF_ABSOLUTE|CsSharpApi.MOUSEEVENTF_MOVE, x, y, 0, 0);
            else CsSharpApi.mouse_event(CsSharpApi.MOUSEEVENTF_MOVE, x, y, 0, 0);
        }
        public static void InputText(string text) {
            SendKeys.SendWait(text);
        }
    }
}
